<?php

class TwoDimStaTableMaker{
	var $groupByColumns = array();
	var $explanColumn ;
	var $explanColumnStatus = array();
	var $countColumns = array() ;
	var $columnStatistics = array() ;
	var $querySql = "" ;
	var $result ;
	var $size = 0 ;
	var $totalSum = 0 ;
	var $data ;

	function TwoDimStaTableMaker(){
		
	}
	
	function init(){
		$this->result = get_result($this->querySql,MYSQL_ASSOC);
		if(empty($this->result)){
			return ;
		}
		$columns = array_keys($this->result[0]);
		$keyColumn = $columns[0] ;
		$this->data = array();
		for($i=0;$i<count($this->result);$i++){
			$line = $this->result[$i];
			$key = $line[$keyColumn];
			$matrix = null ;
			if(array_key_exists($key,$this->data)){
				$matrix = $this->data[$key];
			}else{
				$matrix = array();
			}
			for($t=0;$t<count($this->groupByColumns);$t++){
				$column = $this->groupByColumns[$t]["name"] ;
				$matrix[$column] = $line[$column];
			}
			$explanValue = $line[$this->explanColumn];

			for($t=0;$t<count($this->countColumns);$t++){
				$column = $this->countColumns[$t]["name"] ;
				$countValue = $line[$column];

				$temp = $explanValue . ":" . $column ;
				$string = $key . ":" . $temp ;
				
				$this->totalSum += $countValue ;
				$sta = mget($this->columnStatistics,$temp,0);
				$sta += $countValue ;
				$this->columnStatistics[$temp] = $sta ;
				
				$matrix[$string] = $countValue ;
			}
			// array_push($this->data, $matrix);
			$this->data[$key] = $matrix ;
		}
	}

	function makeHtml(){
		$html  = "<table class='table_list'>";
		$html .= $this->makeTableTitle() ;
		$html .= $this->makeTableLines();
		$html .= $this->makeTableFoot();
		$html .= "</table>";
		return $html ;
	}

	function makeTableTitle(){
		$html = "<tr>";

		$values = array_values($this->groupByColumns);
		for($i=0;$i<count($values);$i++){
			$column = $values[$i];
			$html .= $this->makeOneTitleCol($column);
		}
		
		for($i=0;$i<count($this->explanColumnStatus);$i++){
			$value = $this->explanColumnStatus[$i];
			$label = label($this->explanColumn . ":" . $value) ;
			$column = array("label"=>$label,"width"=>"100","order_key"=>"");
			$html .= $this->makeOneTitleCol($column);
		}

		$temp  = array("label"=>"合计","width"=>"100","order_key"=>"");
		$html .= $this->makeOneTitleCol($temp);
		$html .= "</tr>";
		return $html ;
	}

	function makeTableFoot(){
		$html = "<tr>";
		$values = array_values($this->groupByColumns);
		for($i=0;$i<count($values);$i++){
			$column = $values[$i];
			$html .= $this->makeOneEmptyCol();
		}
		
		for($i=0;$i<count($this->explanColumnStatus);$i++){
			$value = $this->explanColumnStatus[$i];
			$column = $this->countColumns[0];
			$key = $value . ":" . $column["name"] ;
			$staValue = mget($this->columnStatistics,$key,"0");
			if(empty($staValue)){
				$staValue = "0";
			}
			$html .= $this->makeOneSumCol($staValue);
		}

		$temp  = array("label"=>"");
		$html .= $this->makeOneSumCol($this->totalSum);
		$html .= "</tr>";
		return $html ;
	}

	function makeOneTitleCol($column){
		$sort = empty($column["order_key"]) ? "" : " onclick=\"setOrder('".$column["order_key"]."')\"" ;
		$html = "
			<td class='col_title' width='".$column["width"]."'>
				<span class='spanSort' $sort>".$column["label"]."</span>
			</td>
		";
		return $html ;
	}

	function makeOneSumCol($value){
		$html = "
			<td class='col_sum'>
				<span class=''>".number_format($value*1.0)."</span>
			</td>
		";
		return $html ;
	}

	function makeOneEmptyCol(){
		$html = "
			<td class='col_l'>
			</td>
		";
		return $html ;
	}

	function makeTableLines(){
		$html = "";

		if(is_array($this->data)){
			$keys = array_keys($this->data);
			for($i=0;$i<count($keys);$i++){
				$key = $keys[$i];
				$line = $this->data[$key];
				$html .= "<tr class='row_0' onmouseover='mouseOverRow(this)'>";
				
				$columns = $this->groupByColumns;
				for($t=0;$t<count($columns);$t++){
					$column = $columns[$t]["name"];
					$html .= $this->makeOneValueCol($line[$column]);
				}

				$values = $this->explanColumnStatus;
				$total = 0 ;
				for($t=0;$t<count($values);$t++){
					$value = $values[$t];
					$prefix = $key . ":" . $value ;
					
					for($n=0;$n<count($this->countColumns);$n++){
						$column = $this->countColumns[$n]["name"] ;
						$fullname = $prefix . ":" . $column ;

						$count = 0 ;
						if(array_key_exists($fullname,$line)){
							$count  = $line[$fullname] ;
							$total += $count ;
						}
						$html .= $this->makeOneValueCol($count,1,"col_r");
					}
				}
				$html .= $this->makeOneValueCol($total,1,"col_r");
				
				$html .= "</tr>";
			}
		}

		return $html ;
	}

	function makeOneValueCol($value,$format=false,$css='col_l'){
		if($format){
			$value = number_format($value);
		}
		return "<td class='$css'>".$value."</td>";
	}

	function setGroupByColumns($columns){
		$this->groupByColumns = $columns ;
	}
	function setExplanColumn($column){
		$this->explanColumn = $column ;
	}
	function setExplanColumnStatus($status){
		$this->explanColumnStatus = $status ;
	}
	function setCountColumns($columns){
		$this->countColumns = $columns ;
	}
	function setQuerySql($sql){
		$this->querySql = $sql ;
	}
}

?>